package com.weapons.week0903_weapons.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import com.weapons.week0903_weapons.R;
import com.weapons.week0903_weapons.activity.RecommendContentActivity;
import com.weapons.week0903_weapons.bean.RecommentBean;
import com.weapons.week0903_weapons.constant.UrlString;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/21.
 */
public class RecommentTopView extends RelativeLayout implements ViewPager.OnPageChangeListener{
    private ViewPager vp_top;
    private List<RecommentBean.SlideBean> mDatas;
    private LinearLayout ll_point_group;
    private Context mContext;
    private List<View> mImgs;
    private TopBannerAdapter adapter;
    private int mLastIndex;
    private TextView tv_title;

    public RecommentTopView(Context context) {
        this(context,null);
    }

    public RecommentTopView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public RecommentTopView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext=context;
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.recomment_top,this);
        vp_top = (ViewPager) findViewById(R.id.vp_top);
        tv_title= (TextView) findViewById(R.id.tv_title);
        ll_point_group = (LinearLayout) findViewById(R.id.ll_point_group);
        mDatas = new ArrayList<>();
        mImgs = new ArrayList<>();
        adapter = new TopBannerAdapter();
        vp_top.setAdapter(adapter);
        vp_top.addOnPageChangeListener(this);

    }
    public void setData(List<RecommentBean.SlideBean> mDatas){
        if(mDatas==null){
            this.mDatas=null;
            return;
        }
       // this.mDatas.clear();
        this.mDatas.addAll(mDatas);
       // mImgs.clear();
        for (int i = 0; i < mDatas.size(); i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.recomment_top_vp, null);
            ImageView iv_top_img = (ImageView) view.findViewById(R.id.iv_top_img);
            TextView tv_top_title = (TextView) view.findViewById(R.id.tv_top_title);
            Picasso.with(mContext).load(mDatas.get(i).getImage()).into(iv_top_img);
            tv_top_title.setText(mDatas.get(i).getTitle());
            mImgs.add(view);
        }
        for (int i = 0; i < mDatas.size(); i++) {
            ImageView iv_rect = new ImageView(mContext);
            iv_rect.setImageResource(R.drawable.selector_rect);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(40, 10);
            params.setMargins(0,0,5,0);
            iv_rect.setLayoutParams(params);
            //初始化第一次点的状态
            if (i == 0) {
                iv_rect.setEnabled(true);
            } else {
                iv_rect.setEnabled(false);
            }
            ll_point_group.addView(iv_rect);
        }
        adapter.notifyDataSetChanged();
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        ll_point_group.getChildAt(position).setEnabled(true);
        ll_point_group.getChildAt(mLastIndex).setEnabled(false);
        mLastIndex = position;
    }

    @Override
    public void onPageScrollStateChanged(int state) {


    }

    class TopBannerAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return mImgs.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            View view = mImgs.get(position);
            view.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(mContext,RecommendContentActivity.class);
                    intent.putExtra("url",String.format(UrlString.URL_RECOMMEND_CONTENT,mDatas.get(position).getAid()));
                    mContext.startActivity(intent);
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mImgs.get(position));
        }
    }
}
